#!/usr/bin/env python3
# Copyright (C) 2025 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

from collections.abc import Sequence

import pytest

from cmk.ccc.hostaddress import HostName
from cmk.inventory.config import (
    filter_inventory_housekeeping_parameters,
    InvHousekeepingParams,
    InvHousekeepingParamsCombined,
    InvHousekeepingParamsDefaultCombined,
    InvHousekeepingParamsOfHosts,
)


@pytest.mark.parametrize(
    "housekeeping_parameters, hosts_of_site, expected",
    [
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [],
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="no-of-hosts-and-no-hosts-of-site",
        ),
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["hostname"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    )
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [],
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="of-hosts-and-no-hosts-of-site",
        ),
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [HostName("hostname")],
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="no-of-hosts-and-hosts-of-site",
        ),
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["hostname1"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    )
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [HostName("hostname2")],
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="of-hosts-and-hosts-of-site-and-no-match",
        ),
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["hostname"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    )
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [HostName("hostname")],
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["hostname"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    )
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="of-hosts-and-hosts-of-site-and-match",
        ),
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["~h", "~g", "hostname"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    )
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [HostName("hostname")],
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["~h", "hostname"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    )
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="regex-match",
        ),
        pytest.param(
            InvHousekeepingParams(
                for_hosts=[
                    InvHousekeepingParamsOfHosts(
                        regex_or_explicit=["~g"],
                        parameters=(
                            "combined",
                            InvHousekeepingParamsCombined(
                                strategy="and",
                                file_age=1,
                                number_of_history_entries=2,
                            ),
                        ),
                    ),
                ],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            [HostName("hostname")],
            InvHousekeepingParams(
                for_hosts=[],
                default=InvHousekeepingParamsDefaultCombined(
                    strategy="and",
                    file_age=123,
                    number_of_history_entries=456,
                ),
                abandoned_file_age=100,
            ),
            id="no-regex-match",
        ),
    ],
)
def test_filter_inventory_housekeeping_parameters(
    housekeeping_parameters: InvHousekeepingParams,
    hosts_of_site: Sequence[HostName],
    expected: InvHousekeepingParams,
) -> None:
    assert (
        filter_inventory_housekeeping_parameters(
            housekeeping_parameters=housekeeping_parameters,
            host_names=hosts_of_site,
        )
        == expected
    )
